//
//  StrategySubViewController.m
//  WisdomZhuHai
//
//  Created by Admin on 14-10-29.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "StrategySubViewController.h"
#import "ProtocolManager.h"
#import "Common.h"
#import "UIWebControl.h"
#import "StrategyDetailViewController.h"

//static const CGFloat TITLE_VIEW_HEIGHT = 64;
static const CGFloat INTERVAL = 5;

@interface StrategySubViewController ()

@property (nonatomic,strong) NSArray* strategyList;

@property (nonatomic,strong) UIScrollView* contentView;
@property (nonatomic,strong) UIScrollView* topView;
@property (nonatomic,strong) NSMutableDictionary *tag_typeStorager;
@property int selectedViewTag;
//@property (nonatomic,strong) UIView* topView;

@end

@implementation StrategySubViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [Common colorWithRGB:0xdbdcdc];
    self.title = @"攻略列表";

//Bable-
//    CGFloat topWidth = self.view.bounds.size.width - 10;
//    CGFloat topHeight = topWidth/2 + 52 - 5;
//    UIScrollView *topView = [[UIImage alloc] initWithFrame:CGRectMake(5, 5, topWidth, topHeight)];
//    topView.alwaysBounceVertical = YES;
//    [self.view addSubview:topView];
//    self.topView = topView;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 5, self.view.bounds.size.width - 10, self.view.bounds.size.height - 5)];
//Bable-
//    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 5 + topHeight, self.view.bounds.size.width - 10, self.view.bounds.size.height - 10 - topHeight)];
    scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:scrollView];
    self.contentView = scrollView;
    
//    CGFloat halfWidth = (scrollView.bounds.size.width - INTERVAL)/2;
//    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(halfWidth+INTERVAL, 0, halfWidth, TITLE_VIEW_HEIGHT)];
//    titleView.tag = 9999;
//    titleView.backgroundColor = [UIColor blueColor];
//    [scrollView addSubview:titleView];
    
//    UILabel *lbTitle = [[UILabel alloc] initWithFrame:titleView.bounds];
//    lbTitle.backgroundColor = [UIColor clearColor];
//    lbTitle.text = @"季节限定";
//    lbTitle.font = [UIFont boldSystemFontOfSize:22];
//    lbTitle.textAlignment = NSTextAlignmentCenter;
//    lbTitle.textColor = [UIColor whiteColor];
//    [titleView addSubview:lbTitle];
    
    [self getStrategy];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)onTapStrategyView:(UITapGestureRecognizer*)gesture
{
    NSLog(@"onTapStrategyView %d", gesture.view.tag);
    int strategyId = gesture.view.tag;
    [self getContent:strategyId];
//    for (NSDictionary* dic in self.strategyList) {
//        int Id = [[Common valueForKey:P_ID dic:dic] intValue];
//        if (Id == strategyId) {
//            NSString *source = [Common valueForKey:P_SOURCE dic:dic];
//            WebIOS *webCtrl = [[WebIOS alloc] initWithFrame:self.view.bounds url:[Common getUrlByString:source]];
//            [self.navigationController pushViewController:webCtrl animated:YES];
//            return;
//        }
//    }
}

#pragma mark - Protocol request
- (void)getContent:(int)strategyId
{
    self.selectedViewTag = strategyId;
    [[ProtocolManager shareInstance] getContent:strategyId target:self selector:@selector(getContentCallback:)];
}

#pragma mark - Protocol call back
- (void)getContentCallback:(NSDictionary*)dic
{
    NSLog(@"getContentCallback %@", dic);
    if (dic == nil) {
        return;
    }
    NSString *respCode = [Common valueForKey:P_RESPCODE dic:dic];
    if (![respCode isEqualToString:CODE_HANDLE_SUCCESS]) {
        return;
    }
    
    NSString *strategyType = nil;
    strategyType = [self.tag_typeStorager objectForKey:[NSString stringWithFormat:@"%d", self.selectedViewTag]];
    
    if (strategyType == nil) {
        return;
    }
    
    if ([strategyType isEqual:P_TYPE_URL]) {
        int strategyId = [[Common valueForKey:P_ID dic:dic] intValue];
//        NSString *encryptedParams = [NSString stringWithFormat:@"id=%d",strategyId];
//        NSMutableString* url = [NSMutableString stringWithFormat:@"%@/%@/%@", OKAPIBASEURL, APINEWRELEASE, R_GETCONTENT];
//        [url appendFormat:@"?%@", encryptedParams];
        NSMutableString* url = [NSMutableString stringWithFormat:@"%@/%@/%@", OKAPIBASEURL, APISTRATEGY, R_GETCONTENTPAGE];
        [url appendFormat:@"/id/%d", strategyId];
        
        WebIOS *web  = [[WebIOS alloc]initWithFrame:[Common shareInstance].contentFrameNoTabBar url:[Common getUrlByString:url]];
        web.hidesBottomBarWhenPushed = YES;
        web.type = @"NEWS";
        web.target_id = strategyId;
        [[Common shareInstance]stopLoadingView];
        [self.navigationController pushViewController:web animated:YES];
        
    }else if ([strategyType isEqual:P_TYPE_DAYS]){
        StrategyDetailViewController *strategyDetailViewCtrl = [[StrategyDetailViewController alloc] initWithFrame:self.view.bounds];
        strategyDetailViewCtrl.detailInfo = dic;
        int strategyId = [[Common valueForKey:P_ID dic:dic] intValue];
        for (NSDictionary* dic in self.strategyList) {
            int Id = [[Common valueForKey:P_ID dic:dic] intValue];
            if (Id == strategyId) {
                strategyDetailViewCtrl.strategyInfo = dic;
                break;
            }
        }
        [[Common shareInstance]stopLoadingView];
        [self.navigationController pushViewController:strategyDetailViewCtrl animated:YES];
        
    }
    

}

#pragma mark - Private
- (void)setupStrategyViews
{
    for (UIView *view in self.contentView.subviews) {
        if (view.tag != 9999) {
            [view removeFromSuperview];
        }
    }
    
    CGFloat width = (self.contentView.bounds.size.width - INTERVAL)/2;
    CGFloat height = width + 52;
    CGFloat x = 0;
    CGFloat y = 0;
    for (int i=0; i<self.strategyList.count; i++) {
        if (i%2 == 0) {
            x = 0;
        }
        else {
            x = width + INTERVAL;
        }
        y = (i/2)*(height + INTERVAL);

        UIView *view = [self createStrategyView:CGRectMake(x, y, width, height) index:i];
        [self.contentView addSubview:view];
        
        if (i == self.strategyList.count-1) {
            self.contentView.contentSize = CGSizeMake(0, y + INTERVAL + height);
        }
    }
}

- (UIView*)createStrategyView:(CGRect)frame index:(int)index
{
    if (index >= self.strategyList.count) {
        return nil;
    }
    NSDictionary *dic = self.strategyList[index];
    int strategyId = [[Common valueForKey:P_ID dic:dic] intValue];
    NSString *name = [Common valueForKey:P_NAME dic:dic];
    NSString *square_photo = [Common valueForKey:P_SQUARE_PHOTO dic:dic];
    NSString *type = [Common valueForKey:P_TYPE dic:dic];
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.tag = strategyId;
    view.backgroundColor = [UIColor whiteColor];
    view.layer.borderWidth = 0.5;
    view.layer.borderColor = [UIColor grayColor].CGColor;
    
//    [self.tag_typeStorager addValue:type forKey:[NSString stringWithFormat:@"%d",view.tag]];
    NSDictionary *typeDic = [NSDictionary dictionaryWithObject:type forKey:[NSString stringWithFormat:@"%d",view.tag]];
    [self.tag_typeStorager addEntriesFromDictionary:typeDic];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapStrategyView:)];
    [view addGestureRecognizer:tapGesture];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, frame.size.width-10, frame.size.width-10)];
    imgView.tag = 1000;
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    [imgView setImageWithURL:[Common getFullDownloadPathByRelativePath:square_photo] placeholderImage:[Common getPlaceholderImage:ePIT_Strategy]];
    [view addSubview:imgView];
    
    UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(6, frame.size.width+5, frame.size.width-12, frame.size.height-frame.size.width-20)];
    lbTitle.tag = 1001;
    lbTitle.backgroundColor = [UIColor clearColor];
    lbTitle.font = [UIFont systemFontOfSize:13];
    lbTitle.textColor = [UIColor blackColor];
    lbTitle.text = name;
    lbTitle.numberOfLines = 0;
    [view addSubview:lbTitle];
    
//    UILabel *lbDesc = [[UILabel alloc] initWithFrame:CGRectMake(8, frame.size.width+30, frame.size.width-16, 12)];
//    lbDesc.tag = 1002;
//    lbDesc.backgroundColor = [UIColor clearColor];
//    lbDesc.font = [UIFont systemFontOfSize:12];
//    lbDesc.textColor = [UIColor whiteColor];
//    [view addSubview:lbDesc];
    
    return view;
}

#pragma mark - Protocol request
- (void)getStrategy
{
    [[ProtocolManager shareInstance] getStrategy:[NSNumber numberWithInt:self.classifyId] target:self selector:@selector(getStrategyCallback:)];
}

#pragma mark - Protocol call back
- (void)getStrategyCallback:(NSDictionary*)dic
{
    NSLog(@"getStrategyCallback %@", dic);
    if (dic == nil) {
        return;
    }
    id list = [Common valueForKey:P_LIST dic:[Common valueForKey:@"data" dic:dic]];
    if (![list isKindOfClass:[NSArray class]]) {
        return;
    }
    self.strategyList = list;
    self.tag_typeStorager =[[NSMutableDictionary alloc] init];
    [self setupStrategyViews];
    [[Common shareInstance]stopLoadingView];
}

@end
